﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Data;
using System.IO;

using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security.Permissions;
using DotNetNuke.Services.FileSystem;


namespace DotNetNuke.Modules.UserDefinedTable
{



  public sealed class Utilities
  {
    public static bool CBoolN(object value, bool @default = false)
    {
      bool flag;

      if (value == null)
        return @default;
      
      if (value == DBNull.Value)
        return @default;
      
      try
      {
        flag = bool.Parse((string)value);
      }
      catch (Exception e)
      {  
        flag = false; 
      }

      return flag;
    }




    public static int CIntN(object value)
    {
      return CIntN(value, Null.NullInteger);
    }




    public static int CIntN(object value, int @default)
    {
      int num2;

      if (value == null)
        return @default;

      if (value == DBNull.Value)
        return @default;

      if (int.TryParse((string)value, out num2))
        return num2;

      return Null.NullInteger;
    }




    public static string CStrN(object value, string @default = "")
    {
      if ((value != DBNull.Value) && !string.IsNullOrEmpty((string)value))
        return (string)value;
      
      return @default;
    }




    public static string CStrN(DataRow row, string columnname, string @default = "")
    {
      object objectValue = null;

      if (row.Table.Columns.Contains(columnname))
        objectValue = row[columnname];
      
      return CStrN(objectValue, @default);
    }





    public static FolderInfo GetFolder(PortalSettings portalsettings, string HomeFolderName)
    {
      return GetFolder(portalsettings, HomeFolderName, HomeFolderName);
    }





    public static FolderInfo GetFolder(PortalSettings portalsettings, string HomeFolderName, string DefaultFolderName)
    {
      FolderInfo folder = FileSystemUtils.GetFolder(portalsettings.PortalId, HomeFolderName);
      if (folder == null)
      {
        FileSystemUtils.AddFolder(portalsettings, portalsettings.HomeDirectoryMapPath, DefaultFolderName, 0);
        folder = FileSystemUtils.GetFolder(portalsettings.PortalId, DefaultFolderName);
      }
      return folder;
    }




    public static bool hasWritePermission(string folder, int portalid)
    {
      FolderInfo info = new FolderController().GetFolder(portalid, folder, false);

      if (!folder.StartsWith(Definition.XSLFolderName) && !FolderPermissionController.CanAdminFolder(info))
        return false;
      
      return true;
    }




    public static bool SaveScript(int portalID, string fileContent, string fileName, FolderInfo folder, bool ForceOverWrite)
    {
      if (!(ForceOverWrite | !File.Exists(folder.PhysicalPath + fileName)))
        return false;
      
      byte[] bytes = new UTF8Encoding().GetBytes(fileContent);
      FileSystemUtils.SaveFile(folder.PhysicalPath + fileName, bytes);
      FileController controller = new FileController();

      if (controller.GetFile(fileName, portalID, folder.FolderID) != null)
        controller.DeleteFile(portalID, fileName, folder.FolderID, true);
      
      controller.AddFile(portalID, fileName, fileName.Substring(fileName.IndexOf(".") + 1), (long)Null.NullInteger, Null.NullInteger, Null.NullInteger, "text/xml", folder.FolderPath, folder.FolderID, false);
      return true;
    }





    public static string UrlHexDecode(string value)
    {
      int num = 0;
      int num2 = 0;
      int num3 = 0;

      byte[] bytes = new byte[((value.Length / 2) - 1) + 1];
      while (value.Length > (num + 1))
      {
        num3 = Convert.ToInt32(value.Substring(num, 2), 0x10);
        bytes[num2] = Convert.ToByte(num3);
        num += 2;
        num2++;
      }
      return Encoding.UTF8.GetString(bytes);
    }




    public static string UrlHexEncode(string value)
    {
      return BitConverter.ToString(Encoding.UTF8.GetBytes(value)).Replace("-", "");
    }





    public static bool ValidateRegEx(string value, string pattern)
    {
      Regex regex = new Regex(pattern);
      return regex.IsMatch(value);
    }



  }

}

